/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * RedisService.java created on 2020年2月1日 下午8:29:25 by Lyon Lu 
 */
package com.mvc.server.component;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.mvc.server.miaosha.common.CommonUtil;
import com.mvc.server.miaosha.redis.KeyPrefix;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年2月1日 下午8:29:25
 *
 * </pre>
 */
@Service
public class RedisService {
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * Description:
	 * 		设置缓存
	 * @param prefix
	 * @param suffix
	 * @param value
	 * @return boolean      
	 * @throws
	 */
	public Boolean set(KeyPrefix prefix, String suffix, Object value) {
		
		String valStr = CommonUtil.beanToString(value);
		if (StringUtils.isBlank(valStr)) {
			return false;
		}
		int expireSeconds = prefix.expireSeconds();
		String cacheKey = prefix.getPrefix() + suffix;
		if (expireSeconds > 0) {
			stringRedisTemplate.opsForValue().set(cacheKey, valStr, expireSeconds, TimeUnit.SECONDS);
		}
		else {
			stringRedisTemplate.opsForValue().set(cacheKey, valStr);
		}
		
		return true;
	}
	
	/**
	 * Description:
	 * 		获取缓存
	 * @param prefix
	 * @param suffix
	 * @param clazz
	 * @return T      
	 * @throws
	 */
	public <T> T get(KeyPrefix prefix, String suffix, Class<T> clazz) {
		
		String cacheKey = prefix.getPrefix() + suffix;
		
		String str = stringRedisTemplate.opsForValue().get(cacheKey);
		
		return CommonUtil.stringToBean(str, clazz);
	}
	
	public Boolean delete(KeyPrefix prefix, String suffix)
    {
		String cacheKey = prefix.getPrefix() + suffix;
        return stringRedisTemplate.delete(cacheKey);
    }
	
	public Boolean hasKey(KeyPrefix prefix, String suffix) {
		String cacheKey = prefix.getPrefix() + suffix;
		return stringRedisTemplate.hasKey(cacheKey);
    }
	
	public Long incr(KeyPrefix prefix, String suffix, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        
        String cacheKey = prefix.getPrefix() + suffix;
        
        return stringRedisTemplate.opsForValue().increment(cacheKey, delta);
    }
	
	public Long decr(KeyPrefix prefix, String suffix, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        
        String cacheKey = prefix.getPrefix() + suffix;
        
        return stringRedisTemplate.opsForValue().increment(cacheKey, - delta);
    }
	
	public Boolean setIfAbsent(KeyPrefix prefix, String suffix, Object value) {
		
		String valStr = CommonUtil.beanToString(value);
		if (StringUtils.isBlank(valStr)) {
			return false;
		}
		int expireSeconds = prefix.expireSeconds();
		String cacheKey = prefix.getPrefix() + suffix;
		if (expireSeconds > 0) {
			
//			stringRedisTemplate.opsForValue().setIfAbsent(cacheKey, valStr, expireSeconds, TimeUnit.SECONDS);
			
			return true;
		}
		else {
			return stringRedisTemplate.opsForValue().setIfAbsent(cacheKey, valStr);
		}
	}
}
